﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Resources;
using System.Windows;
using System.Windows.Controls;
using Converter;
using Csla.Xaml;
using Accounting.Library;
using Telerik.Windows;
using Telerik.Windows.Controls.GridView;
using Utility;

namespace csla4Skeleton.UI.SL.NghiepVu.BanSanPham.BSP_BanDichVu
{
    public partial class BSP_BanDichVu_ListView : UserControl, Csla.Xaml.ISupportNavigation
    {

        #region ISupportNavigation Members

        public bool CreateBookmarkAfterLoadCompleted
        {
            get { return true; }
        }

        public event EventHandler LoadCompleted;

        protected virtual void OnLoadCompleted()
        {
            if (LoadCompleted != null)
                LoadCompleted(this, EventArgs.Empty);
        }

        public void SetParameters(string parameters)
        {
            // no parameters
        }

        public string Title
        {
            get { return "MuaHangHoa List"; }
        }

        #endregion

        #region Load Methods
        public BSP_BanDichVu_ListView()
        {
            LocalizationConverter converter = new LocalizationConverter();
            converter.LocalResourceManager = new ResourceManager("csla4Skeleton.UI.SL.NghiepVu.BanSanPham.BSP_BanDichVu.Resources", Assembly.GetExecutingAssembly());
            this.Resources.Add("Localization", converter);

            InitializeComponent();

            this.BSP_BanDichVu_GridView.AddHandler(GridViewCellBase.CellDoubleClickEvent, new EventHandler<RadRoutedEventArgs>(OnCellDoubleClick), true);
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
            {
                System.Windows.Data.CollectionViewSource BSP_BanDichVu_ListCollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["BSP_BanDichVu_ListViewModelSource"];
                BSP_BanDichVu_ListViewModel BSP_BanDichVu_ListViewModel = new BSP_BanDichVu_ListViewModel(BusinessConfiguration.Configuration.GridViewFetchMode);
                BSP_BanDichVu_ListCollectionViewSource.Source = new List<object> { BSP_BanDichVu_ListViewModel };
            }
        }
        #endregion

        #region View Model Related Methods
        private BSP_BanDichVu_ListViewModel GetViewModel()
        {
            System.Windows.Data.CollectionViewSource BSP_BanDichVu_ListCollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["BSP_BanDichVu_ListViewModelSource"];
            return ((List<object>)BSP_BanDichVu_ListCollectionViewSource.Source)[0] as BSP_BanDichVu_ListViewModel;
        }

        private void AddItemButton_Click(object sender, RoutedEventArgs e)
        {
            LoadOrderMainWindow(ParentChild1LoadMainWindowMode.New);
        }

        private void EditItemButton_Click(object sender, RoutedEventArgs e)
        {
            LoadOrderMainWindow(ParentChild1LoadMainWindowMode.Edit);
        }

        private void DeleteItemButton_Click(object sender, RoutedEventArgs e)
        {
            Xoa_BSP_BanDichVu();
        }

        private void OnCellDoubleClick(object sender, RoutedEventArgs args)
        {
            LoadOrderMainWindow(ParentChild1LoadMainWindowMode.Edit);
        }

        // Load order main window
        private void LoadOrderMainWindow(ParentChild1LoadMainWindowMode mode)
        {
            if (mode == ParentChild1LoadMainWindowMode.Edit)
            {
                if (this.BSP_BanDichVu_GridView.SelectedItem != null)
                {
                    BSP_BanDichVuInfo info = (BSP_BanDichVuInfo)this.BSP_BanDichVu_GridView.SelectedItem;
                    string parameter = ((BSP_BanDichVuInfo)this.BSP_BanDichVu_GridView.SelectedItem).Khoa.ToString() + ";";
                    if (info.DaGhiSo)
                    {
                        parameter += "DaGhiSo;";
                    }
                    else 
                    {
                        parameter += "ChuaGhiSo;";
                    }
                    if (info.LaChiPhiBanHang && info.HoaDonBanId != null)
                    {
                        parameter += info.MaHoaDonBan;
                    }
                    Navigator.Current.Navigate("csla4Skeleton.UI.SL.NghiepVu.BanSanPham.BSP_BanDichVu.BSP_BanDichVu_MainWindow,csla4Skeleton.UI.SL", parameter);
                }
            }
            else if (mode == ParentChild1LoadMainWindowMode.New)
            {
                Navigator.Current.Navigate("csla4Skeleton.UI.SL.NghiepVu.BanSanPham.BSP_BanDichVu.BSP_BanDichVu_MainWindow,csla4Skeleton.UI.SL");
            }
        }

        private void Xoa_BSP_BanDichVu()
        {
            if (this.BSP_BanDichVu_GridView.SelectedItem != null)
            {
                BSP_BanDichVuInfo info = (BSP_BanDichVuInfo)this.BSP_BanDichVu_GridView.SelectedItem;
                if (info.DaGhiSo)
                {
                    MessageBox.Show("Chứng từ đã ghi sổ. Không được phép xóa.");
                }
                else if (info.LaChiPhiBanHang && info.HoaDonBanId != null)
                {
                    MessageBox.Show("Đã được phân bổ chi phí ở hóa đơn " + info.MaHoaDonBan + ". Không được phép xóa.");
                }
                else
                {
                    GetViewModel().DeleteBSP_BanDichVu(info.Khoa);
                }
            }
        }
        #endregion
    }
}
